package cn.sfa.common.pagination;
/**
 * 
* 类名称：PageLabel   
* 类描述：分页标签类
* 创建人：lenovo   
* 创建时间：2012-2-21 下午3:13:24   
* 修改人：lenovo   
* 修改时间：2012-2-21 下午3:13:24   
* 修改备注：   
* @version
 */
public class PageLabel {

	private int totalNum; // 总记录数

	private int pageNum; // 每页显示记录数,手动设置

	private int totalPage;// 总页数

	private int page;// 当前页
	
	private String url="page";

	public void init(int totalNum, int pageNum, int totalPage, int page,String url) {

		this.totalNum = totalNum; // 总记录数

		this.pageNum = pageNum;

		this.totalPage = totalPage;

		this.page = page;
		
		this.url = url;

	}

	public String getDisplay() {
		String string = "";
		if ((page < 1) || (page > totalPage))
			page = 1;
		if(totalPage>0){
			// 小于十页
			if (totalPage <= 10) {
				if (page == 1) {// 等于首页
					if (page == totalPage) {// 只有一页，可以考虑不显示在界面上。
						// $string += '<a href="test.action?page=1>"[首页]</a> 1 ';
					} else {
						string = "<a href=\""+url+"currentPage=1\">[首页]</a> 1 ";
						for (int i = 2; i <= totalPage; i++) {
							string += "<a href=\""+url+"currentPage=" + i + "\">["
									+ i + "]</a>";
						}
						string += "<a href=\""+url+"currentPage=" + (page + 1)
								+ "\">[下一页]</a>";
					}
				}
	
				else if (page == totalPage) {// 等于尾页
					string += "<a href=\""+url+"/1\">[首页]</a><a href=\""+url+"currentPage="
							+ (page - 1) + "\">[上一页]</a>";
					for (int i = 1; i < totalPage; i++) {
						string += "<a href=\""+url+"currentPage=" + i + "\">["
								+ i + "]</a>";
					}
					string += totalPage+"&nbsp;&nbsp;";
				} else {
					string += "<a href=\""+url+"currentPage=1\">[首页]</a><a href=\""+url+"currentPage="
							+ (page - 1) + "\">[上一页]</a>";
					for (int i = 1; i <= totalPage; i++) {
						if (i == page)
							string += i;
						else
							string += "<a href=\""+url+"currentPage=" + i + "\">["
									+ i + "]</a>";
					}
	
					string += "<a href=\""+url+"currentPage=" + (page + 1)
							+ "\">[下一页]</a>";
				}
			}
			// 大于十页的
			else {
				if (page == 1) {// 等于首页
					string = "<a href=\""+url+"currentPage=1\">[首页]</a> 1 ";
					for (int i = 2; i <= 10; i++) {
						string += "<a href=\""+url+"currentPage=" + i + "\">[" + i
								+ "]</a>";
					}
					string += "<a href=\""+url+"currentPage=" + (page + 1)
							+ "\">[下一页]</a><a href=\""+url+"currentPage="
							+ (totalPage) + "\">[尾页]</a>";
				} else if ((page - 5 >= 0) && (page + 5 <= totalPage)) {// 前面有，后面也有
					string += "<a href=\""+url+"currentPage=1\">[首页]</a><a href=\""+url+"currentPage="
							+ (page - 1) + "\">[上一页]</a>";
					for (int i = page - 4; i < page; i++)
						// 输出前半部分
						string += "<a href=\""+url+"currentPage=" + i + "\">[" + i
								+ "]</a>";
	
					string += page;// 输出当前页
	
					for (int i = page + 1; i < page + 6; i++)
						// 输出后半部分
						string += "<a href=\""+url+"currentPage=" + i + "\">[" + i
								+ "]</a>";
	
					string += "<a href=\""+url+"currentPage=" + (page + 1)
							+ "\">[下一页]</a><a href=\""+url+"currentPage="
							+ (totalPage) + "\">[尾页]</a>";
	
				}
	
				else if (page + 5 > totalPage) {// 后面不够了
					string += "<a href=\""+url+"currentPage=1\">[首页]</a><a href=\""+url+"currentPage="
							+ (page - 1) + "\">[上一页]</a>";
					for (int i = page - 4 - (page + 5 - totalPage); i < page; i++)
						// 输出前半部分
						string += "<a href=\""+url+"currentPage=" + i + "\">[" + i
								+ "]</a>";
	
					string += page;// 输出当前页
	
					for (int i = page + 1; i <= totalPage; i++)
						// 输出后半部分
						string += "<a href=\""+url+"currentPage=" + i + "\">[" + i
								+ "]</a>";
					if (page != totalPage)// 不等于尾页咱就不动它了。
						string += "<a href=\""+url+"currentPage=" + (page + 1)
								+ "\">[下一页]</a>";
	
				} else if (page - 5 < 0) {// 前面不够
					string += "<a href=\""+url+"currentPage=1\">[首页]</a><a href=\""+url+"currentPage="
							+ (page - 1) + "\">[上一页]</a>";
	
					for (int i = 1; i < page; i++)
						// 输出前半部分
						string += "<a href=\""+url+"currentPage=" + i + "\">[" + i
								+ "]</a>";
	
					string += page;// 输出当前页
	
					for (int i = page + 1; i <= 10; i++)
						// 输出后半部分
						string += "<a href=\""+url+"currentPage=" + i + "\">[" + i
								+ "]</a>";
	
					string += "<a href=\""+url+"currentPage=" + (page + 1)
							+ "\">[下一页]</a><a href=\""+url+"currentPage="
							+ (totalPage) + "\">[尾页]</a>";
				}
			}
		}
		return string;
	}
	
	public String getrestDisplay() {
		String string = "";
		if ((page < 1) || (page > totalPage))
			page = 1;

		// 小于十页
		if (totalPage <= 10) {
			if (page == 1) {// 等于首页
				if (page == totalPage) {// 只有一页，可以考虑不显示在界面上。
					// $string += '<a href="test.action?page=1>"[首页]</a> 1 ';
				} else {
					string = "<a href=\""+url+"/1\">[首页]</a> 1 ";
					for (int i = 2; i <= totalPage; i++) {
						string += "<a href=\""+url+"/" + i + "\">["
								+ i + "]</a>";
					}
					string += "<a href=\""+url+"/" + (page + 1)
							+ "\">[下一页]</a>";
				}
			}

			else if (page == totalPage) {// 等于尾页
				string += "<a href=\""+url+"/1\">[首页]</a><a href=\""+url+"/"
						+ (page - 1) + "\">[上一页]</a>";
				for (int i = 1; i < totalPage; i++) {
					string += "<a href=\""+url+"/" + i + "\">["
							+ i + "]</a>";
				}
				string += totalPage+"&nbsp;&nbsp;";
			} else {
				string += "<a href=\""+url+"/1\">[首页]</a><a href=\""+url+"/"
						+ (page - 1) + "\">[上一页]</a>";
				for (int i = 1; i <= totalPage; i++) {
					if (i == page)
						string += i;
					else
						string += "<a href=\""+url+"/" + i + "\">["
								+ i + "]</a>";
				}

				string += "<a href=\""+url+"/" + (page + 1)
						+ "\">[下一页]</a>";
			}
		}
		// 大于十页的
		else {
			if (page == 1) {// 等于首页
				string = "<a href=\""+url+"/1\">[首页]</a> 1 ";
				for (int i = 2; i <= 10; i++) {
					string += "<a href=\""+url+"/" + i + "\">[" + i
							+ "]</a>";
				}
				string += "<a href=\""+url+"/" + (page + 1)
						+ "\">[下一页]</a><a href=\""+url+"/"
						+ (totalPage) + "\">[尾页]</a>";
			} else if ((page - 5 >= 0) && (page + 5 <= totalPage)) {// 前面有，后面也有
				string += "<a href=\""+url+"/1\">[首页]</a><a href=\""+url+"/"
						+ (page - 1) + "\">[上一页]</a>";
				for (int i = page - 4; i < page; i++)
					// 输出前半部分
					string += "<a href=\""+url+"/" + i + "\">[" + i
							+ "]</a>";

				string += page;// 输出当前页

				for (int i = page + 1; i < page + 6; i++)
					// 输出后半部分
					string += "<a href=\""+url+"/" + i + "\">[" + i
							+ "]</a>";

				string += "<a href=\""+url+"/" + (page + 1)
						+ "\">[下一页]</a><a href=\""+url+"/"
						+ (totalPage) + "\">[尾页]</a>";

			}

			else if (page + 5 > totalPage) {// 后面不够了
				string += "<a href=\""+url+"/1\">[首页]</a><a href=\""+url+"/"
						+ (page - 1) + "\">[上一页]</a>";
				for (int i = page - 4 - (page + 5 - totalPage); i < page; i++)
					// 输出前半部分
					string += "<a href=\""+url+"/" + i + "\">[" + i
							+ "]</a>";

				string += page;// 输出当前页

				for (int i = page + 1; i <= totalPage; i++)
					// 输出后半部分
					string += "<a href=\""+url+"/" + i + "\">[" + i
							+ "]</a>";
				if (page != totalPage)// 不等于尾页咱就不动它了。
					string += "<a href=\""+url+"/" + (page + 1)
							+ "\">[下一页]</a>";

			} else if (page - 5 < 0) {// 前面不够
				string += "<a href=\""+url+"/1\">[首页]</a><a href=\""+url+"/"
						+ (page - 1) + "\">[上一页]</a>";

				for (int i = 1; i < page; i++)
					// 输出前半部分
					string += "<a href=\""+url+"/" + i + "\">[" + i
							+ "]</a>";

				string += page;// 输出当前页

				for (int i = page + 1; i <= 10; i++)
					// 输出后半部分
					string += "<a href=\""+url+"/" + i + "\">[" + i
							+ "]</a>";

				string += "<a href=\""+url+"/" + (page + 1)
						+ "\">[下一页]</a><a href=\""+url+"/"
						+ (totalPage) + "\">[尾页]</a>";
			}
		}

		return string;
	}

}
